const App = getApp()
import { OrderModel } from '../../../../models/order'
const orderModel = new OrderModel()
import { formatDate, encodeUnicode } from '../../../../utils/index.js'

Page({
  /**
     * 页面的初始数据
     */
  data: {
    applyRefundInfo: {},
    goodsModel: {},
    urlName: 'weapp/service/',
    questionArr: [
      {
        question: '货品漏发',
        service: ['漏发退款'],
        status: false
      },
      {
        question: '质量问题',
        service: ['退货退款'],
        status: false
      },
      {
        question: '发错规格',
        service: ['退货退款'],
        status: false
      },
      {
        question: '无理由退货',
        service: ['退货退款'],
        status: false
      }
    ],
    activeIndex: null,
    serviceIndex: 0,
    quesStatu: false,
    Imglist: [],
    desc: '',
    reason:'',
    reasonList:['不想要该商品了','尺码不合适','颜色不喜欢','物流太慢了','款式不喜欢','薄厚不合适','其他'],
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    returnId: '', // 售后id
    isSuccess: false // 是否提交成功
  },

  //选择问题
  chooseQue: function(e) {
    var that = this
    var index = parseInt(e.currentTarget.dataset.index)
    //切换选项清除输入内容

    //设置当前样式
    that.setData({
      desc:'',
      activeIndex: index,
      serviceIndex: 0,
      quesStatu: true
    })
  },

  //选择服务
  chooseSer: function(e) {
    var that = this
    var index = parseInt(e.currentTarget.dataset.index)

    //设置当前样式
    that.setData({
      serviceIndex: index
    })
  },

  //获取售后商品信息
  getProveInfo: function(e) {
    const self = this
    const params = {
      orderId: e.orderId,
      orderGoodsId: e.orderGoodsId
      // orderId: '4f10b2df6956480ebb1ef59382f6c053',
      // orderGoodsId:'ccc39a65f28849b582cae9cebe656a55'
    }
    orderModel
      .applyProve(params)
      .then(res => {
        //发货七天内可以申请无理由退款
        //如果当前时间大于订单申请无条件退款的最晚截止时间就隐藏无理由退款
        var timestamp = new Date().getTime()
        if (timestamp <= res.data.applyRefundInfo.refundEnd) {
          self.data.questionArr[0].status = true
          self.data.questionArr[1].status = true
          self.data.questionArr[2].status = true
          self.data.questionArr[3].status = true
          if (timestamp > res.data.applyRefundInfo.noReasonEnd) {
            self.data.questionArr[3].status = false
          }
          self.setData({
            questionArr: self.data.questionArr
          })
        } else {
          self.data.questionArr[1].status = true
          self.setData({
            questionArr: self.data.questionArr
          })
        }
        res.data.applyRefundInfo.realReturn = res.data.applyRefundInfo.returnPrice+res.data.applyRefundInfo.returnFreight
        //结算可退金额
        if(res.data.applyRefundInfo.realReturn <= res.data.applyRefundInfo.goodsNum*1){
          res.data.applyRefundInfo.realReturn = res.data.applyRefundInfo.goodsNum*1
         }
         
        if(res.data.applyRefundInfo.returnPrice <= res.data.applyRefundInfo.goodsNum*1){
        res.data.applyRefundInfo.returnPrice = res.data.applyRefundInfo.goodsNum*1
        }

        self.setData({
          applyRefundInfo: res.data.applyRefundInfo
        })
      })
      .catch(err => {
        console.log(err)
      })
  },

  //用户输入问题描述
  bindKeyInput: function(e) {
    this.setData({
      desc: e.detail.value
    })
  },

  //获取上传的图片信息
  ImgUpload: function(e) {
    this.setData({
      Imglist: e.detail
    })
  },

  //无理由退款理由
  bindPickerChange:function(e){
    this.setData({
      reason:this.data.reasonList[e.detail.value],
      desc: this.data.reasonList[e.detail.value]
    })
  },

  //提交售后
  submit: function(e) {
    const self = this
    //问题描述判断
    if (!self.data.desc) {
      App.WxService.showToast({ title: '请输入问题描述', icon: 'none' })
      return
    }

    //漏发退款、无理由退款不强制上传图片
    if(self.data.activeIndex !=0 && self.data.activeIndex !=3){
      //上传图片为空判断
      if (self.data.Imglist.length < 1) {
        App.WxService.showToast({ title: '请上传问题货品照片', icon: 'none' })
        return
      }
    }
    
    const params = {
      orderId: self.data.goodsModel.orderId,
      orderGoodsId: self.data.goodsModel.orderGoodsId,
      returnType: self.data.activeIndex + 3,
      problemDes: encodeUnicode(self.data.desc),
      returnImgs: self.data.Imglist
    }
    orderModel
      .submitProve(params)
      .then(res => {
        if(params.returnType == 3) {
          App.WxService.showToast({ title: '提交成功', icon: 'none' })
          wx.redirectTo({
            url:
              '/pages/account/service/detail/index?returnId=' + res.data.returnId
          })
        } else {
          self.setData({
            isSuccess: true,
            returnId: res.data.returnId
          })
        }
      })
      .catch()
  },

  // 前往售后详情
  tapToServiceDetail() {
    this.setData({
      isSuccess: false
    })
    wx.redirectTo({
      url:
        '/pages/account/service/detail/index?returnId=' + this.data.returnId
    })
  },
  
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function(options) {
    this.setData({
      goodsModel: options
    })
    this.getProveInfo(options)
  },

  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function() {},

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function() {},

  /**
     * 生命周期函数--监听页面隐藏
     */
  onHide: function() {},

  /**
     * 生命周期函数--监听页面卸载
     */
  onUnload: function() {},

  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function() {},

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function() {}
})
